/**
 *
 */
package com.apache.client;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * @author dyhou
 */
public class WcmCoreClient {

    private static WcmCoreClient instance;

    private static final String serviceName = "websiteService";

    private Logger log = Logger.getLogger(WcmCoreClient.class);

    private WcmCoreClient() {
        //
    }

    public static WcmCoreClient getInstance() {
        if (null == instance) {
            instance = new WcmCoreClient();
        }
        return (instance);
    }

    public ResultMsg saveManuscript(Map map) {
        ResultMsg msg = new ResultMsg("F", "操作失败");
        ResultEntity entity = doService(serviceName, "saveManuscript", map);
        if ("true".equals(entity.getResult()) && StrUtil
                .isNotNull(String.valueOf(entity.getEntity()))) {
            msg = new ResultMsg("T", "操作成功");
        } else {
            msg.setMsg(entity.getMessage());
        }
        return msg;
    }

    /**
     * description:  修改信息状态
     */
    public ResultMsg editInfo(Map map) {
        ResultMsg msg = new ResultMsg("F", "操作失败");
        ResultEntity entity = doService(serviceName, "editManuscriptStatus", map);
        if ("true".equals(entity.getResult()) && StrUtil
                .isNotNull(String.valueOf(entity.getEntity()))) {
            msg = new ResultMsg("T", "操作成功");
        } else {
            msg.setMsg(entity.getMessage());
        }
        return msg;
    }

    /**
     * description:  修改信息
     */
    public Object findManuscripts(Map map) {
        ResultEntity entity = doService(serviceName, "manuscriptList", map);
        log.info("findManuscripts=" + entity.getMessage());
        return entity.getEntity();
    }

    /**
     * description:  删除信息
     */
    public Object findChannels(Map map) {
        ResultEntity entity = doService(serviceName, "channelList", map);
        log.info("findChannels=" + entity.getMessage());
        return entity.getEntity();
    }

    /**
     * description:  查询信息列表
     */
    public Object selectManuscriptInfo(Map map) {
        ResultEntity entity = doService(serviceName, "manuscriptEntity", map);
        log.info("selectManuscriptInfo=" + entity.getMessage());
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            return entity.getEntity();
        }
        return null;
    }

    /**
     * description:  查询单个信息
     */
    public Object selectChannelInfo(Map map) {
        ResultEntity entity = doService(serviceName, "channelEntity", map);
        log.info("selectChannelInfo=" + entity.getMessage());
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            return entity.getEntity();
        }
        return null;
    }

    /**
     * description:  获取稿件内容
     */
    public String getManuscriptContent(Map map) {
        ResultEntity entity = doService(serviceName, "manuscriptContent", map);
        log.info("getManuscriptContent=" + entity.getMessage());
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            return String.valueOf(entity.getEntity());
        }
        return "";
    }

    /**
     * description:  按主键查询会员信息
     */
    public Object getMemberInfo(Map map) {
        ResultEntity entity = doService(serviceName, "memberEntity", map);
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            return entity.getEntity();
        }
        return null;
    }

    private ResultEntity doService(String beanId, String methodKey, Map<String, Object> params) {
        InterfaceRegister register = UctProtoclClient.getInstance()
                .getInterfaceRegister("wcm", "info_release_url", "info_release_port");
        return LoadRpcService.service().doServiceClient(beanId, methodKey, params, register);
    }
}
